@isTest
private class ContractCalcEquipmentUtilTest {
    static testMethod void testMethod1() {
        	
    	    //产品结构
        ProductStructure__c ps1=new ProductStructure__c();
        ps1.Name='仪器';
        ps1.Classify1__c='生化';
        ps1.Classify2__c='生化-设备';
        ps1.Classify3__c='仪器';
        insert ps1;
        Brand__c brand = new Brand__c(
        	BrandShortName__c = '四川迈克'
        );
        insert brand;
       Province__c province  = new Province__c();
       province.Name = '四川';

       insert province ;

         City__c city = new City__c();
       city.Province__c = province.Id;
       insert city;


         Account acc = TestUtils.createAccount();
        acc.Level__c = '三级甲等';
        acc.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('Dealer', 'Account');
          acc.Province__c = province.Id;
        acc.City__c = city.Id;
        acc.DeliveryControl__c = '按季管控';
        insert acc;     

         Dealer__c d = new Dealer__c(
        	Only__c='Dealer__c'
        );
         insert d ;

        //业务机会1
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        insert opp;

        Id pRT = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');

     	Product__c p  = TestUtils.createInst();
        p.Name = '测试仪器';
        p.RecordTypeId = pRT;
        p.Category0__c  = '仪器';
        p.ProductName__c = '测试仪器';
        p.ProductStructure__c = ps1.Id;
        p.CRMBrand__c = brand.Id;
        insert p ;


        Product__c p1  = TestUtils.createInst();
        p1.Name = '测试仪器1';
        p1.RecordTypeId = pRT;
        p1.Category0__c  = '仪器';
        p1.ProductName__c = '测试仪器';
         p1.ProductStructure__c = ps1.Id;
        p1.CRMBrand__c = brand.Id;
        insert p1 ;




        Contract__c con = new Contract__c();
        con.Account__c = acc.Id;
        con.Opportunity__c = opp.Id;
       	con.Status__c  = '已生效';
        con.Corpcode__c = 'MKSY';
        con.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('TerminalContract', 'Contract__c');
        insert con;


          //报价2
        Quote qt2 = new Quote(Name = '试剂报价',Type__c = '试剂报价',OpportunityId = opp.Id,IsActive__c = true);
        qt2.Status = '已批准';
        qt2.Contract__c = con.Id;
        insert qt2 ;

        QuoteProduct__c qp = new QuoteProduct__c(Name = '测试报价产品2',Product__c = p.Id,Quote__c = qt2.Id,SalesPrice__c = 5);
        insert qp ;


        ContractProduct__c cp = new ContractProduct__c();
        cp.Contract__c = con.Id;
        cp.Product__c =p.Id  ;
        cp.Quantity__c = 5;
        cp.OriginNumber__c = 5;
        cp.SalesPrice__c = 1; 
        cp.QuoteProduct__c = qp.Id;
        cp.Opportunity__c  = opp.Id;
        insert cp ;
         ContractCalcEquipmentUtil.calcCoopEquipmentNum(opp.Id);


        //无销售合同
        NoContractInstrumentApplication__c noc =new NoContractInstrumentApplication__c();
        noc.Contract__c = con.Id;
        noc.stage__c = '已批准';
        insert noc;

        NoContractInstrumentApplicationDetail__c nocd = new NoContractInstrumentApplicationDetail__c();
        nocd.ApplicationInstrument__c =  cp.Id; 
        nocd.NoContractInstrumentApplication__c = noc.Id;
        nocd.Number__c = 1; 
        insert nocd ; 


         ContractCalcEquipmentUtil.calcEquipmentNum(con.Id);


        //合同变更 
        Contract__c changeCon = new Contract__c();
        changeCon.Account__c = acc.Id;
        changeCon.Opportunity__c = opp.Id;
       	changeCon.Status__c  = '已生效';
        changeCon.Corpcode__c = 'MKSY';
        changeCon.MainContract__c = con.Id;
        changeCon.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('ChangeContract', 'Contract__c');
        insert changeCon;

        //变更明细
        ContractChangeList__c ccl = new ContractChangeList__c();
        ccl.ChangeQuantity__c = 4;
        ccl.ContractProduct__c = cp.Id;
        ccl.AdjustSalesPrice__c = 10; 
        ccl.AlterType__c = '减少原合同仪器数量';
        ccl.Contract__c = changeCon.Id;
        ccl.Product__c = p.Id;
        insert ccl;


        ContractChangeList__c ccl1 = new ContractChangeList__c();
        ccl1.ChangeQuantity__c = 4;
        ccl1.Product__c = p1.Id ;
        ccl1.AdjustSalesPrice__c = 10; 
        ccl1.AlterType__c = '新增合同仪器';
        ccl1.Contract__c = changeCon.Id;

        insert ccl1;

         System.Test.startTest();


        ContractCalcEquipmentUtil.modifyBackToMainContract(changeCon);

        //发货
        InstrumentDeliveryApplication__c ida = new InstrumentDeliveryApplication__c();
        ida.ReceivingCustomer__c = acc.Id;
        ida.DeliveryRelatedContracts__c = con.Id;
        ida.NC__c = true;


        insert ida ;

        InstrumentDeliverynDetails__c idd = new InstrumentDeliverynDetails__c();
        idd.DeliveryInstrument__c = cp.Id;
        idd.Number__c = 1;
        idd.ReturnToMainTable__c = ida.Id;
        insert idd;

        

        ContractCalcEquipmentUtil.sendBackToContract(opp.Id);


        InstallationApply__c ina = new InstallationApply__c();
        ina.Hospital__c = acc.Id;
        ina.InstallInstruments__c = idd.Id;
        ina.InstallEstiDate__c = Date.newInstance(2017, 12, 1);
        ina.InstallsNumber__c = 1;
        ina.EffectiveTime__c = System.now();
        ina.Instrument__c = p.Id;
        ina.NEWDealer__c = d.Id;

        ina.InstallationType__c = '新装机';
        insert ina; 


      

        Case cas=new Case();
        cas.AccountId=acc.Id;
        cas.InstallationApply__c = ina.Id;
        insert cas;


        WorkOrder__c wo=new WorkOrder__c();
        wo.Case__c=cas.Id;
        wo.Account__c=acc.Id;
        wo.Status__c='处理中';
        wo.ManufactureInfo__c=p1.Id;
        wo.IMEINumber__c = '1245';
        wo.recordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('InstallationOrder','WorkOrder__c');
        wo.WhetherToMakeASingle__c = '否';
        insert wo;
        wo.Status__c = '完成';
        update wo ;


        ContractCalcEquipmentUtil.installBackToContract(cp.Id);

        try{
            for(ContractProduct__c cpN : [select Id ,Quantity__c,InstallQuantity__c from ContractProduct__c where Contract__c =:con.Id ]){
                 cpN.InstallQuantity__c  = cpN.Quantity__c;
                update cpN ;
            }
           
        }catch(Exception e){}



        //退机 
        Return_Machine__c rm = new Return_Machine__c();
        rm.instrument__c  = [select Id from Equipment__c limit 1 ].Id ; 
        rm.Status__c = '已批准';

        insert rm ;
        ContractCalcEquipmentUtil.returnBackToContract(cp.Id);








    }
}